<?php

namespace app\home\controller;

use think\Controller;
use think\Db;
use think\Session;

class Index extends Controller
{
    //右侧公告问题信息
    public function __construct()
    {
        parent::__construct(); //先执行父类构造方法
        $notice = Db::table('notice')->order('time', 'desc')->select();
        $this->assign('notice', $notice);
        $question = Db::table('question')->order('time', 'desc')->select();
        $this->assign('question', $question);
        $this->assign('action', '');
        $this->assign('active', '');
    }

    public function ismoblie()
    {
        $this->view->engine->layout(false);
        return view();
    }

//首页
    public function index()
    {
        $preject = Db::table('preject')->order('time', 'desc')->paginate(6);
        $this->assign('preject', $preject);
        return view();
    }

//内容页面
    public function content()
    {
        if (request()->isGet()) {
            $id = input('get.id');
            $type = input('get.type');
            switch ($type) {
                case 1:
                    $preject = Db::table('preject')->where('id', $id)->select();
                    $this->assign('preject', $preject);
                    $this->assign('type', 1);
                    break;

                case 2:
                    $notice = Db::table('notice')->where('id', $id)->select();
                    $this->assign('notice', $notice);
                    $this->assign('type', 2);
                    break;

                case 3:
                    $question = Db::table('question')->where('id', $id)->select();
                    $this->assign('question', $question);
                    $this->assign('type', 3);
                    break;
            }

        }
        return view();
    }

    //注册页面
    public function register()
    {
        //初始化系部下拉菜单
        $xibu = Db::table('apartment')->where(array('s' => 's'))->select();
        $this->assign('xibu', $xibu);
        //初始化部门下拉菜单
        $apartment = Db::table('apartment')->select();
        $this->assign('apartment', $apartment);
        //初始化年级下拉菜单
        $grade = Db::table('classi')->group("grade")->order('grade asc')->select();
        $this->assign('grade', $grade);
        //判断用户类型，显示不同页面
        if (request()->get()) {
            $type = input('get.type');
            $this->assign('type', $type);
        }
        return view();
    }

// 注册功能
    public function registeraction()
    {
        if (request()->post()) {
            $captcha = input('post.captcha');
            if (!captcha_check($captcha)) {
                //验证码错误
                return 0;
            }
            $type = input('post.type');
            $data['name'] = input('post.name');
            $data['salt'] = substr(uniqid(), -6);
            $data['psw'] = $this->password(input('post.psw'), $data['salt']);
            $data['email'] = input('post.email');
            $data['phone'] = input('post.phone');
            $data['apartment'] = input('post.apartment');
            switch ($type) {
                case 1:
                    $data['stuid'] = input('post.stuid');
                    $data['grade'] = input('post.grade');
                    $data['class'] = input('post.class');
                    $isregister = Db::table('student')->where(array('stuid' => $data['stuid']))->select();
                    if ($isregister) {
                        //用户已经存在
                        return 1;
                    } else {
                        $id = Db::table('student')->insertGetId($data);
                        //注册成功
                        $info = array('type' => 1, 'id' => $id, 'name' => $data['name'], 'stuid' => $data['stuid']);
                        Session::set('info', $info);
                        return 2;
                    }
                    break;

                case 2:
                    $isregister = Db::table('teacher')->where(array('name' => $data['name'], 'phone' => $data['phone']))->select();
                    if ($isregister) {
                        //用户已经存在
                        return 1;
                    } else {
                        $id = Db::table('teacher')->insertGetId($data);
                        //注册成功
                        $info = array('type' => 2, 'id' => $id, 'name' => $data['name']);
                        Session::set('info', $info);
                        return 2;
                    }
                    break;
                case 3:
                    $isregister = Db::table('outuser')->where(array('name' => $data['name'], 'phone' => $data['phone']))->select();
                    if ($isregister) {
                        //用户已经存在
                        return 1;
                    } else {
                        $id = Db::table('outuser')->insertGetId($data);
                        //注册成功
                        $info = array('type' => 3, 'id' => $id, 'name' => $data['name']);
                        Session::set('info', $info);
                        return 2;

                    }
                    break;

            }
        }
    }

//登陆页面
    public function login()
    {
        if (request()->get()) {
            $type = input('get.type');
            $this->assign('type', $type);
        }
        return view();
    }

    //登陆页面
    public function m_login()
    {
        if (request()->get()) {
            $type = input('get.type');
            $this->assign('type', $type);
        }
        return view();
    }

//登陆功能
    public function loginaction()
    {
        if (request()->post()) {
            $captcha = input('post.captcha');
            if (!captcha_check($captcha)) {
                //验证码错误
                return 0;
            }
            $type = input('post.type');
            $psw = input('post.psw');
            $phone = input('post.phone');
            switch ($type) {
                case 1:
                    $stuid = input('post.stuid');
                    $data = Db::table('student')->field('id,name,stuid,psw,salt')->where(array('stuid' => $stuid))->find();
                    if (!$data) {
                        //用户不存在
                        return 1;
                    } else {
                        if ($data['psw'] == $this->password($psw, $data['salt'])) {
                            //登陆成功
                            $info = array('type' => 1, 'id' => $data['id'], 'name' => $data['name'], 'stuid' => $data['stuid']);
                            Session::set('info', $info);
                            return 2;
                        } else {
                            //账号或密码错误
                            return 3;
                        }
                    }


                    break;

                case 2:
                    $data = Db::table('teacher')->field('id,name,psw,salt')->where(array('phone' => $phone))->find();
                    if (!$data) {
                        //用户不存在
                        return 1;
                    } else {
                        if ($data['psw'] == $this->password($psw, $data['salt'])) {
                            //登陆成功
                            $info = array('type' => 2, 'id' => $data['id'], 'name' => $data['name']);
                            Session::set('info', $info);
                            return 2;
                        } else {
                            //账号或密码错误
                            return 3;
                        }
                    }


                    break;
                case 3:
                    $data = Db::table('student')->field('id,name,psw,salt')->where(array('phone' => $phone))->find();
                    if (!$data) {
                        //用户不存在
                        return 1;
                    } else {
                        if ($data['psw'] == $this->password($psw, $data['salt'])) {
                            //登陆成功
                            $info = array('type' => 3, 'id' => $data['id'], 'name' => $data['name']);
                            Session::set('info', $info);
                            return 2;
                        } else {
                            //账号或密码错误
                            return 3;
                        }
                    }

                    break;

            }
        }

    }

//找回密码页面
    public function find()
    {
        if (request()->get()) {
            $type = input('get.type');
            $this->assign('type', $type);
        }
        return view();
    }

//验证信息功能
    public function findaction()
    {
        if (request()->port()) {
            $captcha = input('post.captcha');
            if (!captcha_check($captcha)) {
                //验证码错误
                return 0;
            }
            $type = input('post.type');
            $stuid = input('post.stuid');
            $name = input('post.name');
            $email = input('post.email');
            $phone = input('post.phone');
            switch ($type) {
                case 1:
                    $result = Db::table('student')->field('id,name')->where(array('stuid' => $stuid, 'email' => $email, 'phone' => $phone))->find();
                    if ($result) {
                        //验证成功
                        $info = array('type' => 1, 'id' => $result['id']);
                        Session::set('info', $info);
                        return 1;
                    } else {
                        //验证失败
                        return 2;
                    }

                    break;

                case 2:
                    $result = Db::table('teacher')->field('id,name')->where(array('name' => $name, 'email' => $email, 'phone' => $phone))->find();
                    if ($result) {
                        //验证成功
                        $info = array('type' => 2, 'id' => $result['id']);
                        Session::set('info', $info);
                        return 1;
                    } else {
                        //验证失败
                        return 2;
                    }
                    break;

                case 3:
                    $result = Db::table('outuser')->field('id,name')->where(array('name' => $name, 'email' => $email, 'phone' => $phone))->find();
                    if ($result) {
                        //验证成功
                        $info = array('type' => 3, 'id' => $result['id']);
                        Session::set('info', $info);
                        return 1;
                    } else {
                        //验证失败
                        return 2;
                    }
                    break;
            }


        }
    }

//修改密码页面
    public function update()
    {
        if (!Session::has('info')) {
            $this->redirect('index');
        }

        return view();
    }

//修改密码功能
    public function updateaction()
    {
        if (request()->port()) {
            $type = Session::get('info.type');
            $id = Session::get('info.id');
            $data['salt'] = substr(uniqid(), -6);
            $data['psw'] = $this->password(input('post.psw'), $data['salt']);
            switch ($type) {
                case 1:
                    Db::table('student')->where(array('id' => $id))->update($data);
                    return 1;
                    break;

                case 2:
                    Db::table('teacher')->where(array('id' => $id))->update($data);
                    return 1;
                    break;

                case 3:
                    Db::table('outuser')->where(array('id' => $id))->update($data);
                    return 1;
                    break;
            }
        }
    }

    //个人中心页面
    public function center()
    {
        if (!Session::has('info')) {
            $this->redirect('index');
        } else {
            $info = Db::table('person')->where('stuid', Session::get('info.stuid'))->select();
            $team = Db::table('team')->where('stuid', Session::get('info.stuid'))->select();
            $this->assign('info', $info);
            $this->assign('team', $team);
        }
        return view();
    }

    public function delete()
    {
        $id = input('get.id');
        if (input('get.type') == 1) {
            Db::table('person')->where('id', $id)->delete();
        } else {
            Db::table('team')->where('id', $id)->delete();
        }
        $this->redirect('center');
    }

    //判断是否登陆
    public function con()
    {
        if (!Session::has('info')) {
            return 0;
        } else {
            return 1;
        }
    }

    //确认报名页面
    public function confirm()
    {
        if (request()->get()) {
            $t = input('get.t');
            $this->assign('t', $t);
            $id = input('get.id');
            $preject = Db::table('preject')->field('id,title,endtime')->where(array('id' => $id))->find();
            $endtime = strtotime($preject['endtime']);
            $nowtime = time();
            $cha = $nowtime - $endtime;
            if ($cha > 0) {
                $this->error("报名时间已截止！");
            }

            $this->assign('preject', $preject);
            $type = Session::get('info.type');
            $id = Session::get('info.id');

            switch ($type) {
                case 1:
                    $userinfo = Db::table('student')->where(array('id' => $id))->select();
                    $this->assign('userinfo', $userinfo);
                    break;
                case 2:
                    $userinfo = Db::table('teacher')->where(array('id' => $id))->select();
                    $this->assign('userinfo', $userinfo);
                    break;
                case 3:
                    $userinfo = Db::table('outuser')->where(array('id' => $id))->select();
                    $this->assign('userinfo', $userinfo);
                    break;
            }

        }
        return view();
    }

    //确认报名功能
    public function confirmaction()
    {
        if (request()->post()) {
            $t = input('post.t');
            $data['uid'] = input('post.uid');
            $data['preject'] = input('post.preject');
            $data['name'] = input('post.name');
            $data['stuid'] = input('post.stuid');
            $data['apartment'] = input('post.apartment');
            $data['grade'] = input('post.grade');
            $data['class'] = input('post.class');
            $data['email'] = input('post.email');
            $data['phone'] = input('post.phone');
            switch ($t) {
                case 1:
                    $isalredy = Db::table('person')->where(array('uid' => $data['uid'], 'stuid' => $data['stuid']))->select();
                    if ($isalredy) {
                        //重复报名
                        return 0;
                    } else {
                        Db::table('person')->insert($data);
                        return 1;
                    }
                    break;
                case 2:
                    $isalredy = Db::table('team')->where(array('uid' => $data['uid'], 'stuid' => $data['stuid']))->select();
                    if ($isalredy) {
                        //重复报名
                        return 0;
                    } else {
                        Db::table('team')->insert($data);
                        return 1;
                    }
                    break;
            }


        }

    }

    public function myinfo()
    {
        $type = Session::get('info.type');
        $id = Session::get('info.id');
        switch ($type) {
            case 1:
                $userinfo = Db::table('student')->where(array('id' => $id))->select();
                $this->assign('userinfo', $userinfo);
                break;
            case 2:
                $userinfo = Db::table('teacher')->where(array('id' => $id))->select();
                $this->assign('userinfo', $userinfo);
                break;
            case 3:
                $userinfo = Db::table('outuser')->where(array('id' => $id))->select();
                $this->assign('userinfo', $userinfo);
                break;
        }
        if (request()->post()) {
            $data['name'] = input('post.name');
            $data['email'] = input('post.email');
            $data['phone'] = input('post.phone');
            $data['apartment'] = input('post.apartment');
            switch ($type) {
                case 1:
                    $data['stuid'] = input('post.stuid');
                    $data['grade'] = input('post.grade');
                    $data['class'] = input('post.class');
                    Db::table('student')->where('id', $id)->update($data);
                    //更新成功
                    return 1;
                    break;
                case 2:
                    Db::table('teacher')->where('id', $id)->update($data);
                    //用户已经存在
                    return 1;
                    break;
                case 3:
                    Db::table('outuser')->where('id', $id)->update($data);
                    //用户已经存在
                    return 1;
                    break;

            }
        }
        return view();
    }

//退出登陆
    public function logout()
    {
        Session::clear();
        $this->redirect('index');
    }

//学生注册获取对应班级
    public function getclass()
    {
        $grade = input('post.grade');
        $uid = input('post.uid');
        $where = array('grade' => $grade, 'uid' => $uid);
        $info = Db::table('classi')->where($where)->column('classname');

        foreach ($info as $v) {
            echo " <option value=$v>$v</option>";
        }
    }


    //密码加密函数
    private function password($psw, $salt)
    {
        return md5(md5($psw) . $salt);
    }

}
